﻿@inject IHttpService _httpService

@inject NavigationManager NavigationManager
@inject IPopupService PopupService

<MDCard>
    <DataTableCard TModel="FavoriteObjectOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl+Id)" ShowAddButton="false" Title="@($"{_name}列表")" @ref="dataTableCard">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <MIcon Color="success"
                       Class="mr-2"
                       OnClick="()=>MoveItem(context.Item)">@IconType.Move.ToIconString()</MIcon>
                <MIcon Color="error"
                       Class="mr-2"
                       OnClick="()=>DeleteItem(context.Item)">@IconType.Delete.ToIconString()</MIcon>
            }
            else if (context.Header.Value == nameof(FavoriteObjectOverviewModel.Type))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Favorites.Objects.ObjectChip Name=@context.Item.ObjectName Id="@context.Item.ObjectId" Type="@context.Item.Type" />
            }
            else if (context.Header.Value == nameof(FavoriteObjectOverviewModel.CreateTime))
            {
                @context.Item.CreateTime.ToString("yyyy-MM-dd HH:mm")
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>

<MoveFavoriteFolderTip Id="Id" OnMoved="@( () => { dataTableCard.GetDataFromApi(); })" @ref="moveFavoriteFolderTip" />
@code {
    [Parameter]
    public long Id { get; set; }

    private string _baseUrl = "api/Favorites/ListUserFavoriteObjects?folderId=";
    private string _name = "收藏夹";

    public List<FavoriteFolderOverviewModel> UserFavoriteInfor { get; set; }


    private List<DataTableHeader<FavoriteObjectOverviewModel>> _headers = new List<DataTableHeader<FavoriteObjectOverviewModel>>
    {
        new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(FavoriteObjectOverviewModel.Id)
          },
          new (){ Text= "名称", Value= nameof(FavoriteObjectOverviewModel.Type)},
          new (){ Text= "时间", Value= nameof(FavoriteObjectOverviewModel.CreateTime)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<FavoriteObjectOverviewModel> dataTableCard;
    MoveFavoriteFolderTip moveFavoriteFolderTip;


    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override async Task OnInitializedAsync()
    {
        try
        {
            UserFavoriteInfor = await _httpService.GetAsync<List<FavoriteFolderOverviewModel>>("api/favorites/GetUserFavoriteInforFromFolderId/" + Id);
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取用户收藏夹失败");
        }
    }

    public void MoveItem(FavoriteObjectOverviewModel model)
    {
        List<KeyValuePair<FavoriteObjectType, long>> ObjectIds = new List<KeyValuePair<FavoriteObjectType, long>>();
        ObjectIds.Add(new KeyValuePair<FavoriteObjectType, long>(model.Type, model.ObjectId));


        if (moveFavoriteFolderTip != null)
        {
            moveFavoriteFolderTip.Refresh(ObjectIds, UserFavoriteInfor);
        }

    }

    public async Task DeleteItem(FavoriteObjectOverviewModel model)
    {
        string operationName ="删除"+_name;
        try
        {
            var obj = await _httpService.PostAsync<DeleteFavoriteObjectsModel, Result>("api/favorites/UserDeleteFavoriteObject", new DeleteFavoriteObjectsModel { FavorieFolderId = Id, Ids = new long[] { model.Id} });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败", AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }

}
